#include "attribute_handle.h"

void attribute_handle_data(u8 *data,u16 len,DATA_SOURCE source)
{

    printf("source %d\n",source);
    
    if (source == TCP_SOURCE)
    {
        set_rec_message(&data[16]);
        ACK_report();
    }
    
    for(int i=20;i<len;) //for循环缺省判断
    {
        u32 target=0;
        target |= data[i+0];
        target |= data[i+1] << 8;
        target |= data[i+2] << 16;
        target |= data[i+3] << 24;
        printf("i is %lx\n",target);

        switch (target)
        {
            case HIGH_SPEED: //高速模式包
                u8 num17 = (data[i+5]<<8|data[i+4]);
                LOG_INFO("high spped data is %d\n",num17);
                set_high_speed_handle(num17,&data[i+6]);
                i+=(4+2+num17);
                break;
                
            case REGISTER: //重发注册包
                send_register_package();
                i+=4;
                break;
            
            case INQUIRE: //查询指令
                iot_report_init();
                i +=4;
                break;
            
            case RGB_BRIGHNESS:  //RGB灯的亮度
                u8 num2 = data[i+4]; 
                RGB_brighness_handle(num2);
                i +=(4+1);
                LOG_INFO("set RGB brighness is%d,i is %d\n",num2,i);
                break;   
            case LEVEL_CW: //CW 色温
                u8 num18 =data[i+4]; 
                CW_level_handle(num18);
                i +=(4+1);
                break;
            
            case BRIGHNESS_CW: //CW 亮度
                u8 num19 =data[i+4]; 
                CW_brighness_handle(num19);
                i +=(4+1);
                break;
            case MAIN_MODE: //主模式切换
                u8 num4 = data[i+4]; //主模式值
                main_mode_handle(num4);
                i+=(4+1);
                LOG_INFO("set main mode is%d,i is %d\n",num4,i);
                break;

            case OPEN_CLOSE:
                u8 num15 = data[i+4];
                if(num15 ==0x01)
                {
                    app_logic_set_ssp(SSP_POWER_OFF);
                }
                else if(num15 == 0x02)
                {
                    app_logic_set_ssp(app_logic_get_recover_ssp());
                }
                i+=(4+1);
                LOG_INFO("set OPEN_CLOSE is%d,i is %d\n",num15,i);
                break;
            
            case SCENE_MODE: //场景模式
                u8 num10 = data[i+4];
                set_scene_handle(num10);
                LOG_INFO("SCENE_MODE %d\n",num10);
                i+=(4+1);
                break;  
            
            case SCENE_SENST: //场景模式灵敏度
                u8 num16 = data[i+4];
                i+=(4+1);
                set_scene_senet_handle(num16);
                LOG_INFO("SCENE_SENET %d\n",num16);
                break;
                
            case CUSTOM_SSP: //自定义模式子模式
                u8 num12 = data[i+4];
                set_custom_ssp_handle(num12);
                LOG_INFO("CUSTOM_SSP %d\n",num12);
                i+=(4+1);
                break;

            case EFFECT_SSP: //自定义模式预览灯效
                u8 num11 = data[i+4];
                set_light_effect_enum_hanle(num11);
                LOG_INFO("EFFECT_SSP %d\n",num11);
                i+=(4+1);
                break;

            case CUSTOM_WAIT:  //自定义延时时间
                u8 num13 = data[i+4];
                set_custom_light_wait_handle(num13);
                LOG_INFO("CUSTOM_WAIT %d\n",num13);
                i+=(4+1);
                break;

            case CUSTOM_PART_DATA:  //自定义模式分段信息
                light_effect_config.len = data[i+4];
                LOG_INFO("light_effect_config.len %d\n",light_effect_config.len*3);
                memset(light_effect_config.buf,0,45);
                memcpy(light_effect_config.buf,&data[i+5],light_effect_config.len*3);
                i+=(4+1+light_effect_config.len*3);
                break;

            case CUSTOM_SAVE: //自定义保存模式
                u8 num14 = data[i+4];
                custom_write_partition(num14,&data[i+5]);
                custom_save_report();
                set_custom_ssp(num14);
                LOG_INFO("CUSTOM_SAVE %d\n",num14);
                i+=(4+1+48);
                break;    

            case MICRO_SENST: //麦克风模式灵敏度
                u8 num5 = data[i+4];
                i+=(4+1);
                set_senst_level_hamdle(num5);
                LOG_INFO("set micro senst is%d,i is %d\n",num5,i);
                break;
            
            case SPECTRAL_MODE: //音乐频谱模式
                u8 num9 = data[i+4];
                i+=(4+1);
                set_micro_ssp_hamdle(num9);
                LOG_INFO("set spectral mode is%d,i is %d\n",num9,i);
                break;

            case PART_DATA: //分段信息
                part_info.len = data[i+4];
                memcpy(part_info.RGB,&data[i+5],part_info.len*3);
                i+=(4+1+45);
                break;

            case NET_RESET: //重置网络信息
                wifi_logic_set_ssp(BULFI_OPEN);
                net_reset_report();
                LOG_INFO("NET_RESET\n");
                i+=4;
                break;

            case OTA: //OTA模式
                u16 num6 = (u16)(data[i+4+1]|data[i+4]);
                memcpy(ota_data,&data[i+4+2],num6);
                ota_data[num6] ='\0';
                printf("num6 %d\n",num6);
                printf("ota_data %s\n",ota_data);
                init_http_ota();
                i+=(4+2+2+num6);
                break;
            default:
                LOG_INFO("Instruction number parsing error\n");
                i+=1024;
                break;
        }
    }
}